package com.nowcoder.community.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.nowcoder.community.util.CommunityUtils;
import com.nowcoder.community.util.Constants;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = true)
public class SystemConversation extends Conversation {

    @TableField(exist = false)
    private List<Notice> notices;

    @TableField(exist = false)
    private Map<String, Object> contentMap;

    public String getNoticeIconUrl() {
        if (Constants.SYSTEM_NOTICE_COMMENT.equals(getId())) {
            return "http://static.nowcoder.com/images/head/reply.png";
        }
        if (Constants.SYSTEM_NOTICE_LIKE.equals(getId())) {
            return "http://static.nowcoder.com/images/head/like.png";
        }
        if (Constants.SYSTEM_NOTICE_FOLLOW.equals(getId())) {
            return "http://static.nowcoder.com/images/head/follow.png";
        }
        return "#";
    }

    private void readJsonContent() {
        if (contentMap == null) {
            try {
                contentMap = CommunityUtils.parseJson(getMessage().getContent());
            } catch (JsonProcessingException e) {
                throw new RuntimeException("无法将" + getMessage().getContent() + "转化为Map对象.");
            }
        }
    }

    public int getTargetUserId() {
        readJsonContent();
        return (int) contentMap.get("userId");
    }

    public int getEntityType() {
        readJsonContent();
        return (int) contentMap.get("entityType");
    }
}
